<?php

/**
 * 权限管理
 *
 * @author JiangJian <silverd@sohu.com>
 * @copyright 2011-2012 xiangle.com
 * $Id: Privilege.php 162 2012-11-09 04:04:30Z Jun.Lu.726@gmail.com $
 * @version    2012-02-15  ::  JiangJian  ::  Create File
 */
class Controller_System_Privilege extends Controller_Abstract
{

    private $_priv;

    public function init()
    {
        parent::init();
        $this->_priv = Model('System_Privilege');
    }

    /**
     * 权限颗粒列表
     */
    public function indexAction()
    {
        // 已激活的权限列表
        $privList = $this->_priv->getAssocList();

        // 扫描文件系统的所有 Action 列表
        $scanList = $this->_priv->getMethodListFromFileSystem();

        // echo "<pre>";
//var_dump($privList);
//var_dump($scanList);
//exit;
        $this->assign(array(
            'privList' => $privList,
            'scanList' => $scanList,
        ));
        //$this->render('system/priv_list');
    }

    /**
     * 添加权限颗粒
     */
    public function addAction()
    {
        if (!$this->isPost()) {
            exit('Access Deined');
        }

        $data   = $this->getPost();
        $result = $this->_priv->add($data);
        if ($result !== true) {
            $this->jsonx($result, 'error');
        }

        $this->jsonx('添加权限成功', 'success');
    }

    /**
     * 更新权限名
     */
    public function updateAction()
    {
        if (!$this->isPost()) {
            exit('Access Deined');
        }

        $privId = intval($this->get('priv_id'));
        if ($privId < 1) {
            $this->jsonx('非法的权限id', 'error');
        }

        $theOne = $this->_priv->rowByPk($privId);
        if (!$theOne) {
            $this->jsonx('权限信息不存在', 'error');
        }

        $data   = $this->getPost();
        $result = $this->_priv->update1($privId, $data);
        if ($result !== true) {
            $this->jsonx($result, 'error');
        }

        $this->jsonx('修改权限成功', 'success');
    }

    /**
     * 删除权限颗粒
     */
    public function deleteAction()
    {
        $privId = intval($this->get('priv_id'));
        if ($privId < 1) {
            $this->jsonx('非法的权限id', 'error');
        }

        if (!$this->_priv->deleteByPk($privId)) {
            $this->jsonx('删除权限失败', 'error');
        }

        $this->jsonx('删除权限成功', 'success');
    }

}